package yf.mall.service.impl;

import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import yf.mall.common.Const;
import yf.mall.dao.*;
import yf.mall.entity.*;
import yf.mall.enums.AccountTypeEnum;
import yf.mall.enums.CashApplyStatusEnum;
import yf.mall.service.*;
import com.yf.service.impl.BaseServiceImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 提现申请表Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Mon Jul 06 11:27:01 CST 2020
 */
@Service
public class CashApplyServiceImpl extends BaseServiceImpl<CashApplyEntity> implements CashApplyService {

    private CashApplyDao cashApplyDao;


    @Autowired
    public void setDao(CashApplyDao cashApplyDao) {
        this.cashApplyDao = cashApplyDao;
        super.setBaseDao(cashApplyDao);
    }


    /**
     *
     *   @Override
     *     public List<UserVo> queryListVo(UserVo userVo) {
     *         return userDao.queryListVo(userVo);
     *     }
     *
     *     @Override
     *     public UserVo getVo(Long id) {
     *         return userDao.getVo(id);
     *     }

     @Override List<CashApplyVo> queryListVo(CashApplyVo cashApplyVo){
     return cashApplyDao.queryListVo(cashApplyVo);
     }

     @Override CashApplyVo getVo(Long id){
     return cashApplyDao.getVo(id);
     }
     **/


    /**内部方法**/


    /**
     * 外部方法实现
     **/

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private AccountRecordService accountRecordService;


    @Autowired
    private UserDao userDao;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private LevelDao levelDao;

    /**
     * Long
     * <p>
     * 提现申请
     *
     * @param userId
     * @param cashApplyEntity
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void apply(Long userId, CashApplyEntity cashApplyEntity) {
        UserEntity userEntity = this.userDao.get(userId);
        LevelEntity level = this.levelDao.findByUserId(userId);
        if (level.getLv().equals(this.levelDao.findBase().getLv())) {
            throw new RRException("游客不能提现");
        }
        AccountEntity account = this.accountDao.findByUserIdAndType(userId, AccountTypeEnum.CASH.getIndex());
        cashApplyEntity.setStatus(CashApplyStatusEnum.AUDIT.getIndex());
        cashApplyEntity.setCrateTime(new Date());
        String no = createNO(userId);
        cashApplyEntity.setUserId(userId);
        cashApplyEntity.setAppNo(no);
        this.cashApplyDao.save(cashApplyEntity);
        if (account == null || (account.getAmount().compareTo(BigDecimal.ZERO) <= 0) || (cashApplyEntity.getAmount().compareTo(BigDecimal.ZERO) <= 0) || (account.getAmount().subtract(cashApplyEntity.getAmount()).compareTo(BigDecimal.ZERO) < 0)) {
            throw new RRException("账户余额不足");
        }
        accountService.transfer(userId, Const.ADMIN_ID, AccountTypeEnum.CASH.getIndex(), cashApplyEntity.getAmount(), userEntity.getName() + "申请提现了" + cashApplyEntity.getAmount() + "￥,编号:" + cashApplyEntity.getAppNo());
        this.noticeService.sendApplyCash(userEntity.getOpenId(), cashApplyEntity.getAmount().toString(), cashApplyEntity.getCrateTime(), "商城提现", userEntity.getName() + "申请提现:" + cashApplyEntity.getAmount() + "元,编号:" + cashApplyEntity.getAppNo());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCashApply(CashApplyEntity cashApply) {
        CashApplyEntity cashApplyEntity = this.cashApplyDao.get(cashApply.getId());
        if (cashApplyEntity.getStatus().equals(CashApplyStatusEnum.REFUSE.getIndex()) || cashApplyEntity.getStatus().equals(CashApplyStatusEnum.PASS.getIndex())) {
            throw new RRException("该申请已通过或已拒绝");
        }
        UserEntity userEntity = userDao.get(cashApply.getUserId());
        cashApply.setPassTime(new Date());
        this.cashApplyDao.update(cashApply);
        if (cashApply.getStatus().equals(CashApplyStatusEnum.PASS.getIndex())) {
            this.noticeService.sendApplyCashStatus(userEntity.getOpenId(), cashApply.getAmount().toString(), cashApply.getCrateTime(),cashApply.getPassTime(),"普通提现","审核通过", "您的提现申请已通过,已转银行处理，具体到账时间以银行为准，请注意查收~~");
        } else if (cashApply.getStatus().equals(CashApplyStatusEnum.REFUSE.getIndex())) {
            this.accountService.transfer(Const.ADMIN_ID, cashApply.getUserId(), AccountTypeEnum.CASH.getIndex(), cashApply.getAmount(), "该申请没有通过审核,钱已退还到账,编号:" + cashApply.getAppNo());
            this.noticeService.sendApplyCashStatus(userEntity.getOpenId(),"", cashApply.getCrateTime(),new Date(),"普通提现","审核失败","您的提现申请未通过,编号:" + cashApply.getAppNo());
        }
    }

    //创建提现编号
    private String createNO(Long userId) {
        return System.currentTimeMillis() + "" + Math.round(Math.random() * 100) + "" + userId;
    }



}
